package com.bite.redisDemo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;

public class RedisDemoZset {

    public static void test1(Jedis jedis){
        System.out.println("zadd 和 zrange 和 zrangeWithScores");
        jedis.flushAll();

        jedis.zadd("key1",93,"张三");
        // zset: 有序,默认是升序
        HashMap<String,Double> map = new HashMap<>();
        map.put("王五",95.0);
        map.put("李四",94.0);
        jedis.zadd("key1",map);

        List<String> zrangeResult = jedis.zrange("key1", 0, -1);
        System.out.println("zrangeResult: " + zrangeResult);
        System.out.println("zset 默认是升序排序");

        // Tuple: 元组
        List<Tuple> zrangeWithScoresResult = jedis.zrangeWithScores("key1", 0, -1);
        System.out.println("zrangeWithScoresResult: " + zrangeWithScoresResult);

        // 若只想获取value
        String element = zrangeWithScoresResult.get(0).getElement();
        // 若只想获取分数
        double score = zrangeWithScoresResult.get(0).getScore();

        System.out.println("element: "+element);
        System.out.println("score: "+score);
    }

    public static void test2(Jedis jedis){
        System.out.println("zcard: 获取 value 个数");
        jedis.flushAll();

        jedis.zadd("key",13,"zhangsan");
        jedis.zadd("key",14,"lisi");
        jedis.zadd("key",15,"wangwu");
        jedis.zadd("key",16,"zhaoliu");

        long zcardResult = jedis.zcard("key");
        System.out.println("zcardResult: " + zcardResult);
    }

    public static void test3(Jedis jedis){
        System.out.println("zrem: 删除元素");
        jedis.flushAll();

        jedis.zadd("key",13,"zhangsan");
        jedis.zadd("key",14,"lisi");
        jedis.zadd("key",15,"wangwu");
        jedis.zadd("key",16,"zhaoliu");
        System.out.println(jedis.zrangeWithScores("key", 0, -1));

        long zremResult = jedis.zrem("key", "zhangsan","lisi");
        System.out.println("zremResult: " + zremResult);

        System.out.println(jedis.zrangeWithScores("key", 0, -1));
    }

    public static void test4(Jedis jedis){
        System.out.println("zscore: 根据 member 获取 score");
        jedis.flushAll();

        jedis.zadd("key",13,"zhangsan");
        jedis.zadd("key",14,"lisi");
        jedis.zadd("key",15,"wangwu");
        jedis.zadd("key",16,"zhaoliu");

        Double zscoreResult = jedis.zscore("key","zhangsan");
        System.out.println("zscoreResult: "+zscoreResult);

        Double zscoreResultZhangSan2 = jedis.zscore("key","zhangsan2");
        System.out.println("zscoreResultZhangSan2: " + zscoreResultZhangSan2);

    }

    public static void test5(Jedis jedis){
        System.out.println("zrank: 获取排名(元素下标)");
        jedis.flushAll();

        jedis.zadd("key",13,"zhangsan");
        jedis.zadd("key",14,"lisi");
        jedis.zadd("key",15,"wangwu");
        jedis.zadd("key",16,"zhaoliu");

        Long zrank = jedis.zrank("key", "zhangsan");
        System.out.println("zrankZhangsan: " + zrank);
        System.out.println("lisi: "+jedis.zrank("key", "lisi"));
        System.out.println("lisi2 "+jedis.zrank("key", "lisi2"));
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }catch (Exception e){
        }
    }
}
